//
//  BluetoothCenter+Editor.m
//  Holyiot-DFU
//
//  Created by bill on 2022/2/22.
//

#import "BluetoothCenter+Editor.h"
#import "LBTool.h"
@implementation BluetoothCenter (Editor)
#pragma mark ----- 修改蓝牙名 -------
-(void)modifyDeviceName:(NSString*)deviceName;
{
    NSData *nameData = [deviceName dataUsingEncoding:NSUTF8StringEncoding];
    Byte len = nameData.length;
    //命令的头部
    Byte headBytes[] = {0xf3,0x05,0xf3,len};
    NSData *headData = [[NSData alloc]initWithBytes:headBytes length:sizeof(headBytes)];
    [self sendDataWithHeadData:headData secondData:nameData];
}
#pragma mark ----- 修改设备密码 十六进制-------
-(void)modifyDevicePwd:(NSString*)password
{
    NSData *pwdData = [LBTool convertBytesStringToData:password leng:6 fillPosition:FillPositionBehind];
    Byte len = pwdData.length;
    //命令的头部
    Byte headBytes[] = {0xf3,0x01,0xf3,len};
    NSData *headData = [[NSData alloc]initWithBytes:headBytes length:sizeof(headBytes)];
    [self sendDataWithHeadData:headData secondData:pwdData];
}
#pragma mark --------- 修改major ---------
-(void)modifyMajor:(NSString*)major
{
    //将十进制转成十六进制字符串
    NSString *hexStr = [LBTool convertDecimalismStrToHexStr:major];
    //十六进制字符串转data
    NSData *major_data = [LBTool convertBytesStringToData:hexStr leng:2 fillPosition:FillPositionFront];
    Byte len = major_data.length;
    //命令的头部
    Byte headBytes[] = {0xf3,0x03,0xf3,len};
    NSData *headData = [[NSData alloc]initWithBytes:headBytes length:sizeof(headBytes)];
    NSMutableData *finalData = [NSMutableData new];
    [finalData appendData:headData];
    //如果不足两个字节,在 前面 补00
    if (major_data.length==1) {
        Byte zero[] = {0x00};
        [finalData appendData:[[NSData alloc]initWithBytes:zero length:sizeof(zero)]];
    }
    [finalData appendData:major_data];
    [self sendDataToDevice:finalData];
    
}
#pragma mark --------- 修改minor ---------
-(void)modifyMinor:(NSString*)minor
{
    //将十进制转成十六进制字符串
    NSString *hexStr = [LBTool convertDecimalismStrToHexStr:minor];
    //将十六进制转成data
    NSData *minor_data = [LBTool convertBytesStringToData:hexStr leng:2 fillPosition:FillPositionFront];
    Byte len = minor_data.length;
    //命令的头部
    Byte headBytes[] = {0xf3,0x04,0xf3,len};
    NSData *headData = [[NSData alloc]initWithBytes:headBytes length:sizeof(headBytes)];
    NSMutableData *finalData = [NSMutableData new];
    [finalData appendData:headData];
    //如果不足两个字节,在前面补00
    if (minor_data.length==1) {
        Byte zero[] = {0x00};
        [finalData appendData:[[NSData alloc]initWithBytes:zero length:sizeof(zero)]];
    }
    [finalData appendData:minor_data];
    [self sendDataToDevice:finalData];
}
#pragma mark ------- 修改mac ---------
-(void)modifyMAC:(NSString*)mac
{
    
    //不足6个字节的在后面补0,超过6个字节的去除多余的
    //将十六进制转成data
    NSData *mac_data = [LBTool convertBytesStringToData:mac leng:6 fillPosition:FillPositionBehind];
    Byte len = mac_data.length;
    //命令的头部
    Byte headBytes[] = {0xf3,0x09,0xf3,len};
    NSData *headData = [[NSData alloc]initWithBytes:headBytes length:sizeof(headBytes)];
    [self sendDataWithHeadData:headData secondData:mac_data];
}
#pragma mark ------- 修改uuid ---------
-(void)modifyUUID:(NSString*)uuid
{
    //将十六进制转成data
    NSData *uuid_data = [LBTool convertBytesStringToData:uuid leng:16 fillPosition:FillPositionBehind];
    Byte len = uuid_data.length;
    //命令的头部
    Byte headBytes[] = {0xf3,0x02,0xf3,len};
    NSData *headData = [[NSData alloc]initWithBytes:headBytes length:sizeof(headBytes)];
    [self sendDataWithHeadData:headData secondData:uuid_data];
}
#pragma mark ------- 修改发射功率  0-6个档---------
-(void)modifyTX_Power:(int)tx_power
{
    Byte byte[] = {tx_power};
    NSData *tx_data = [NSData dataWithBytes:byte length:sizeof(byte)];
    Byte len = tx_data.length;
    //命令的头部
    Byte headBytes[] = {0xf3,0x06,0xf3,len};
    NSData *headData = [[NSData alloc]initWithBytes:headBytes length:sizeof(headBytes)];
    [self sendDataWithHeadData:headData secondData:tx_data];
}
#pragma mark ------- 修改广播间隔 0-6个档--------
-(void)modifyADV_interval:(int)interval
{
    Byte byte[] = {interval};
    NSData *interval_data = [NSData dataWithBytes:byte length:sizeof(byte)];
    Byte len = interval_data.length;
    //命令的头部
    Byte headBytes[] = {0xf3,0x07,0xf3,len};
    NSData *headData = [[NSData alloc]initWithBytes:headBytes length:sizeof(headBytes)];
    [self sendDataWithHeadData:headData secondData:interval_data];
}
#pragma mark ------- 修改一米处的RSSI
-(void)modifyOneMeterRSSI:(int)rssiValue
{
    //将十六进制转成data
    NSData *rssi_data = [LBTool convertBytesStringToData:[NSString stringWithFormat:@"%d",rssiValue] leng:0 fillPosition:FillPositionFront];
    Byte len = rssi_data.length;
    //命令的头部
    Byte headBytes[] = {0xf3,0x08,0xf3,len};
    NSData *headData = [[NSData alloc]initWithBytes:headBytes length:sizeof(headBytes)];
    [self sendDataWithHeadData:headData secondData:rssi_data];
}
#pragma mark ------- DFU -----
-(void)enterDFU
{
//    Byte headBytes[] = {0xf3,0x0A,0xf3};
    
    NSData *dfuData = [LBTool convertBytesStringToData:self.dfu_code leng:0 fillPosition:FillPositionBehind];
    [self sendDataToDevice:dfuData];
}
#pragma mark -------修改自定义广播数据
-(void)modifyCustomADV_data:(NSString*)hexString
{
    //将十六进制转成data
    NSData *adv_data = [LBTool convertBytesStringToData:hexString leng:0 fillPosition:FillPositionBehind];
    Byte len = adv_data.length;
    //命令的头部
    Byte headBytes[] = {0xf3,0x0b,0xf3,len};
    NSData *headData = [[NSData alloc]initWithBytes:headBytes length:sizeof(headBytes)];
    [self sendDataWithHeadData:headData secondData:adv_data];
}
//控制外部设备
-(void)openExteralDevice:(BOOL)state
{
    NSString *cmd;
    if (state) {
        cmd = @"F317F30101";
    }else{
        cmd = @"F317F30100";
    }
    [self sendHexStringToDevice:cmd];
}
#pragma mark ----------- Eddystone edit -------------------------
//修改UID name
-(void)modifyEddystone_UID_Adv_name:(NSString *)uid_Adv_name
{
    NSData *nameData = [uid_Adv_name dataUsingEncoding:NSUTF8StringEncoding];
    Byte len = nameData.length;
    //命令的头部
    Byte headBytes[] = {0xf3,0x24,0xf3,len};
    NSData *headData = [[NSData alloc]initWithBytes:headBytes length:sizeof(headBytes)];
    [self sendDataWithHeadData:headData secondData:nameData];
}
//修改UID_namespaceID  最大10个字节
-(void)modifyEddystone_UID_namespaceID:(NSString*)uid_namespaceID
{
    //将十六进制转成data
    NSData *uid_namespaceID_data = [LBTool convertBytesStringToData:uid_namespaceID leng:10 fillPosition:FillPositionBehind];
    Byte len = uid_namespaceID_data.length;
    //命令的头部
    Byte headBytes[] = {0xf3,0x25,0xf3,len};
    NSData *headData = [[NSData alloc]initWithBytes:headBytes length:sizeof(headBytes)];
    [self sendDataWithHeadData:headData secondData:uid_namespaceID_data];
}
//修改UID_instanceID  最大6个字节
-(void)modifyEddystone_UID_instanceID:(NSString*)uid_instanceID
{
    //将十六进制转成data
    NSData *uid_instance_data = [LBTool convertBytesStringToData:uid_instanceID leng:6 fillPosition:FillPositionBehind];
    Byte len = uid_instance_data.length;
    //命令的头部
    Byte headBytes[] = {0xf3,0x26,0xf3,len};
    NSData *headData = [[NSData alloc]initWithBytes:headBytes length:sizeof(headBytes)];
    [self sendDataWithHeadData:headData secondData:uid_instance_data];
}
//修改URL 广播名称
-(void)modifyEddystone_URL_Adv_name:(NSString*)url_Adv_name
{
    NSData *nameData = [url_Adv_name dataUsingEncoding:NSUTF8StringEncoding];
    Byte len = nameData.length;
    //命令的头部
    Byte headBytes[] = {0xf3,0x27,0xf3,len};
    NSData *headData = [[NSData alloc]initWithBytes:headBytes length:sizeof(headBytes)];
    [self sendDataWithHeadData:headData secondData:nameData];
}
-(void)modifyEddystone_URL_prefix_address:(NSInteger)type
{
    Byte headBytes[] = {0xf3,0x28,0xf3,0x01,type};
    NSData *headData = [[NSData alloc]initWithBytes:headBytes length:sizeof(headBytes)];
    [self sendDataToDevice:headData];
}
//最大16字节
-(void)modifyEddystone_URL_content:(NSString*)url_content
{
    NSData *url_content_Data = [url_content dataUsingEncoding:NSUTF8StringEncoding];
    Byte len = url_content_Data.length;
    //命令的头部
    Byte headBytes[] = {0xf3,0x29,0xf3,len};
    NSData *headData = [[NSData alloc]initWithBytes:headBytes length:sizeof(headBytes)];
    [self sendDataWithHeadData:headData secondData:url_content_Data];
}
-(void)modifyEddystone_URL_subfix_address:(NSInteger)type
{
    Byte headBytes[] = {0xf3,0x2a,0xf3,0x01,type};
    NSData *headData = [[NSData alloc]initWithBytes:headBytes length:sizeof(headBytes)];
    [self sendDataToDevice:headData];
}
//修改 eddystone TLM 广播名称  最大12字节
-(void)modifyEddystone_TLM_Adv_name:(NSString*)TLM_Adv_name
{
    NSData *nameData = [TLM_Adv_name dataUsingEncoding:NSUTF8StringEncoding];
    Byte len = nameData.length;
    //命令的头部
    Byte headBytes[] = {0xf3,0x2b,0xf3,len};
    NSData *headData = [[NSData alloc]initWithBytes:headBytes length:sizeof(headBytes)];
    [self sendDataWithHeadData:headData secondData:nameData];
}
-(void)modifyEddystone_TLM_version:(NSInteger)TLM_version
{
    Byte headBytes[] = {0xf3,0x2c,0xf3,0x05,TLM_version};
    NSData *headData = [[NSData alloc]initWithBytes:headBytes length:sizeof(headBytes)];
    [self sendDataToDevice:headData];
}
-(void)modifyEddystone_EID_Adv_name:(NSString*)EIM_Adv_name
{
    NSData *nameData = [EIM_Adv_name dataUsingEncoding:NSUTF8StringEncoding];
    Byte len = nameData.length;
    //命令的头部
    Byte headBytes[] = {0xf3,0x2d,0xf3,len};
    NSData *headData = [[NSData alloc]initWithBytes:headBytes length:sizeof(headBytes)];
    [self sendDataWithHeadData:headData secondData:nameData];
}
-(void)changeAdvMode:(NSInteger)mode
{
    Byte switchtype[] = {0xf3,0x2f,0xf3,0x01,mode};
    NSData *data = [NSData dataWithBytes:switchtype length:sizeof(switchtype)];
    [self sendDataToDevice:data];
}
-(void)modifyEddystone_EID_encryptedID:(NSString*)encryptedID
{
    //将十六进制转成data
    NSData *encryptedID_data = [LBTool convertBytesStringToData:encryptedID leng:8 fillPosition:FillPositionBehind];
    Byte len = encryptedID_data.length;
    //命令的头部
    Byte headBytes[] = {0xf3,0x2e,0xf3,len};
    NSData *headData = [[NSData alloc]initWithBytes:headBytes length:sizeof(headBytes)];
    [self sendDataWithHeadData:headData secondData:encryptedID_data];
}
#pragma mark ----- 查询 eddystone 的广播模式
-(void)checkEddystoneAdvState
{
    Byte headBytes[] = {0xf3,0x30,0xf3};
    NSData *headData = [[NSData alloc]initWithBytes:headBytes length:sizeof(headBytes)];
    [self sendDataToDevice:headData];
}
//拼接数据并发送
-(void)sendDataWithHeadData:(NSData*)headData secondData:(NSData*)secondData
{
    NSMutableData *finalData = [NSMutableData new];
    [finalData appendData:headData];
    [finalData appendData:secondData];
    [self sendDataToDevice:finalData];
}

//发送十六进制字符串
-(void)sendHexStringToDevice:(NSString*)cmd
{
    NSData *cmdData = [LBTool convertBytesStringToData:cmd leng:0 fillPosition:FillPositionBehind];
    [self sendDataToDevice:cmdData];
}


@end
